#!/srv/newsblur/venv/newsblur3/bin/python 
import os

import redis

from utils.munin.base import MuninGraph


class NBMuninGraph(MuninGraph):


    def __init__(self, *args, **kwargs):
        super(NBMuninGraph, self).__init__(*args, **kwargs)
        self.host = os.environ.get("REDIS_HOST", "localhost")
    
    @property
    def graph_config(self):
        graph = {
            'graph_category' : 'redis',
            'graph_title' : 'Redis DB sizes',
            'graph_args' : '-l 0',
        }
        
        stats = self.stats
        graph.update(dict((("%s.label" % s, s) for s in stats.keys())))
        graph.update(dict((("%s.draw" % s, 'LINE1') for s in stats.keys())))
        
        return graph

    def calculate_metrics(self):
        return self.stats
    
    @property
    def stats(self):
        r = redis.Redis(self.host)
        info = r.info()
        dbs = dict([(k, v) for k, v in info.items() if k.startswith('db')])
        sizes = {}
        for db, values in dbs.items():
            for key, value in values.items():
                if key == 'avg_ttl': continue
                sizes[db+"_"+key] = value
        return sizes
        

if __name__ == '__main__':
    NBMuninGraph().run()


